﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace ASP.NET.Samples
{
    public partial class Eg_Cookie : System.Web.UI.Page
    {
        protected string CookieHtml;
        protected int CookieCount;

        protected void Page_Load(object sender, EventArgs e)
        {
            txtCookieValue.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            if (!IsPostBack) QueryCookie();
        }

        protected void btnCreate_Click(object sender, EventArgs e)
        {
            Request.Cookies.Add(new HttpCookie(txtCookieKey.Value,txtCookieValue.Value){Domain = txtDomain.Value,Expires = DateTime.Now.AddDays(7),Path = "/",Secure = false});
            QueryCookie();
        }

        protected void btnQuery_Click(object sender, EventArgs e)
        {
            QueryCookie();
        }

        private void QueryCookie()
        {
            CookieHtml = "";
            if (Request.Cookies.Count > 0)
            {
                CookieCount = Request.Cookies.Count;
                foreach (var key in Request.Cookies.AllKeys)
                {
                    CookieHtml += "<tr><td>" + Request.Cookies[key].Name + "</td><td>" + Request.Cookies[key].Value + "</td><td>" + Request.Cookies[key].Domain
                        + "</td><td>" + Request.Cookies[key].Expires.ToString("yyyy MMMM dd") + "</td><td>" + Request.Cookies[key].Path + "</td><td>" + Request.Cookies[key].Secure + "</td></tr>";
                }
            }
        }
    }
}